package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.repository.IAnswerTemplateRepository;
import cn.wolfcode.luowowo.search.service.IAnserTemplateService;
import cn.wolfcode.luowowo.search.template.AnswerTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class IAnswerTempleServiceImpl implements IAnserTemplateService{
    @Autowired
    private IAnswerTemplateRepository dao;
    @Autowired
    private ElasticsearchTemplate template;



    //建立索引库
    public void createIndex() {
        template.createIndex(AnswerTemplate.class);
        template.getMapping(AnswerTemplate.class);
    }
    //保存answerTemple模板
    public void save(AnswerTemplate template) {
        dao.save(template);
    }

    //根据name查询地区
    public List<AnswerTemplate> getAnswerByTtile(String key) {
        Page<AnswerTemplate> title = dao.search(QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("title", key)), PageRequest.of(0, 3));
        List list = new ArrayList();
        /*iterable.forEach(d->{
            list.add(d);
        });*/
        return title.getContent();
    }
}
